/*
 * Copyright (c) 2013. wyouflf (wyouflf@gmail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.dongfang.net.http.client.callback;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;

import com.dongfang.utils.IOUtils;
import com.dongfang.utils.OtherUtils;

public class StringDownloadHandler {

	public String handleEntity(HttpEntity entity, RequestCallBackHandler callBackHandler, String charset)
			throws IOException {
		if (entity == null)
			return null;

		long current = 0;
		long total = entity.getContentLength();

		if (callBackHandler != null && !callBackHandler.updateProgress(total, current, true)) {
			return null;
		}

		InputStream inputStream = null;
		StringBuilder sb = new StringBuilder();
		try {
			inputStream = entity.getContent();
			BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, charset));
			String line = "";
			while ((line = reader.readLine()) != null) {
				sb.append(line);
				current += OtherUtils.sizeOfString(line, charset);
				if (callBackHandler != null) {
					if (!callBackHandler.updateProgress(total, current, false)) {
						return sb.toString();
					}
				}
			}
			if (callBackHandler != null) {
				callBackHandler.updateProgress(total, current, true);
			}
		} finally {
			IOUtils.closeQuietly(inputStream);
		}
		return sb.toString();
	}

}
